library(ggplot2)
library(interplot)
library(stargazer)

setwd("/Users/boramlee/Dropbox/NAFTA_submission/RIPE submission/RIPE_revision")
dat <- read.csv("sidedeal.csv")


################ Outcome Variable rescale

dat$march <- NA

for(i in 1:nrow(dat)){
  if(is.na(dat$guess_March[i]) == TRUE){dat$march[i] <- NA}
  else if(dat$guess_March[i] == 1){dat$march[i] <- 1}
  else if(dat$guess_March[i] == 2){dat$march[i] <- 0.75}
  else if(dat$guess_March[i] == 3){dat$march[i] <- 0.5}
  else if(dat$guess_March[i] == 4){dat$march[i] <- 0.25}
  else if(dat$guess_March[i] == 5){dat$march[i] <- 0}
}

dat$june <- NA

for(i in 1:nrow(dat)){
  if(is.na(dat$guess_June[i]) == TRUE){dat$june[i] <- NA}
  else if(dat$guess_June[i] == 1){dat$june[i] <- 1}
  else if(dat$guess_June[i] == 2){dat$june[i] <- 0.75}
  else if(dat$guess_June[i] == 3){dat$june[i] <- 0.5}
  else if(dat$guess_June[i] == 4){dat$june[i] <- 0.25}
  else if(dat$guess_June[i] == 5){dat$june[i] <- 0}
}



dat$sep <- NA

for(i in 1:nrow(dat)){
  if(is.na(dat$guess_Sept[i]) == TRUE){dat$sep[i] <- NA}
  else if(dat$guess_Sept[i] == 1){dat$sep[i] <- 1}
  else if(dat$guess_Sept[i] == 2){dat$sep[i] <- 0.75}
  else if(dat$guess_Sept[i] == 3){dat$sep[i] <- 0.5}
  else if(dat$guess_Sept[i] == 4){dat$sep[i] <- 0.25}
  else if(dat$guess_Sept[i] == 5){dat$sep[i] <- 0}
}

dat$oct <- NA

for(i in 1:nrow(dat)){
  if(is.na(dat$october_cd[i]) == TRUE){dat$oct[i] <- NA}
  else if(dat$october_cd[i] == 1){dat$oct[i] <- 1}
  else if(dat$october_cd[i] == 2){dat$oct[i] <- 0.75}
  else if(dat$october_cd[i] == 3){dat$oct[i] <- 0.5}
  else if(dat$october_cd[i] == 4){dat$oct[i] <- 0.25}
  else if(dat$october_cd[i] == 5){dat$oct[i] <- 0}
}


dat$nov <- NA

for(i in 1:nrow(dat)){
  if(is.na(dat$november_cd[i]) == TRUE){dat$nov[i] <- NA}
  else if(dat$november_cd[i] == 1){dat$nov[i] <- 1}
  else if(dat$november_cd[i] == 2){dat$nov[i] <- 0.75}
  else if(dat$november_cd[i] == 3){dat$nov[i] <- 0.5}
  else if(dat$november_cd[i] == 4){dat$nov[i] <- 0.25}
  else if(dat$november_cd[i] == 5){dat$nov[i] <- 0}
}



############## Figure 4 

####### March plot
dathousedem <- na.omit(subset(dat, democrat==1 & senate==0, select = march))

ggplot(dathousedem, aes(x=factor(march))) +
  geom_bar(stat="count", width=0.7, fill = "steelblue") +
  theme_minimal() + labs(title="Support for NAFTA among the House Democrats in March 1993", 
                         x="Support for NAFTA (0 - Oppose, 0.25 - Leaning against, \n 0.5 - Undecided, 0.75 - Leaning in favor, 1 - Support)", y = "Count")+
  ylim(0, 100)



dathouserep <- na.omit(subset(dat, democrat==0 & senate==0, select = march))

ggplot(dathouserep, aes(x=factor(march))) +
  geom_bar(stat="count", width=0.7, fill = "red") +
  theme_minimal() + labs(title="Support for NAFTA among the House Republicans in March 1993", 
                         x="Support for NAFTA (0 - Oppose, 0.25 - Leaning against, \n 0.5 - Undecided, 0.75 - Leaning in favor, 1 - Support)", y = "Count")+
  ylim(0, 100)



############### June plot

dathousedem <- na.omit(subset(dat, democrat==1 & senate==0, select = june))

ggplot(dathousedem, aes(x=factor(june))) +
  geom_bar(stat="count", width=0.7, fill = "steelblue") +
  theme_minimal() + labs(title="Support for NAFTA among the House Democrats in June 1993", 
                         x="Support for NAFTA (0 - Oppose, 0.25 - Leaning against, \n 0.5 - Undecided, 0.75 - Leaning in favor, 1 - Support)", y = "Count") +
  ylim(0, 100)



dathouserep <- na.omit(subset(dat, democrat==0 & senate==0, select = june))

ggplot(dathouserep, aes(x=factor(june))) +
  geom_bar(stat="count", width=0.7, fill = "red") +
  theme_minimal() + labs(title="Support for NAFTA among the House Republicans in June 1993", 
                         x="Support for NAFTA (0 - Oppose, 0.25 - Leaning against, \n 0.5 - Undecided, 0.75 - Leaning in favor, 1 - Support)", y = "Count")+
  ylim(0, 100)


############### September plot

dathousedem <- na.omit(subset(dat, democrat==1 & senate==0, select = sep))

ggplot(dathousedem, aes(x=factor(sep))) +
  geom_bar(stat="count", width=0.7, fill = "steelblue") +
  theme_minimal() + labs(title="Support for NAFTA among the House Democrats in Sep 1993", 
                         x="Support for NAFTA (0 - Oppose, 0.25 - Leaning against, \n 0.5 - Undecided, 0.75 - Leaning in favor, 1 - Support)", y = "Count") +
  ylim(0, 100)



dathouserep <- na.omit(subset(dat, democrat==0 & senate==0, select = sep))

ggplot(dathouserep, aes(x=factor(sep))) +
  geom_bar(stat="count", width=0.7, fill = "red") +
  theme_minimal() + labs(title="Support for NAFTA among the House Republicans in Sep 1993", 
                         x="Support for NAFTA (0 - Oppose, 0.25 - Leaning against, \n 0.5 - Undecided, 0.75 - Leaning in favor, 1 - Support)", y = "Count")+
  ylim(0, 100)


############### October plot

dathousedem <- na.omit(subset(dat, democrat==1 & senate==0, select = oct))

ggplot(dathousedem, aes(x=factor(oct))) +
  geom_bar(stat="count", width=0.7, fill = "steelblue") +
  theme_minimal() + labs(title="Support for NAFTA among the House Democrats in Oct 1993", 
                         x="Support for NAFTA (0 - Oppose, 0.25 - Leaning against, \n 0.5 - Undecided, 0.75 - Leaning in favor, 1 - Support)", y = "Count") +
  ylim(0, 130)



dathouserep <- na.omit(subset(dat, democrat==0 & senate==0, select = oct))

ggplot(dathouserep, aes(x=factor(oct))) +
  geom_bar(stat="count", width=0.7, fill = "red") +
  theme_minimal() + labs(title="Support for NAFTA among the House Republicans in Oct 1993", 
                         x="Support for NAFTA (0 - Oppose, 0.25 - Leaning against, \n 0.5 - Undecided, 0.75 - Leaning in favor, 1 - Support)", y = "Count")+
  ylim(0, 130)



############### November plot

dathousedem <- na.omit(subset(dat, democrat==1 & senate==0, select = nov))

ggplot(dathousedem, aes(x=factor(nov))) +
  geom_bar(stat="count", width=0.7, fill = "steelblue") +
  theme_minimal() + labs(title="Support for NAFTA among the House Democrats in Nov 1993", 
                         x="Support for NAFTA (0 - Oppose, 0.25 - Leaning against, \n 0.5 - Undecided, 0.75 - Leaning in favor, 1 - Support)", y = "Count") +
  ylim(0, 150)



dathouserep <- na.omit(subset(dat, democrat==0 & senate==0, select = nov))

ggplot(dathouserep, aes(x=factor(nov))) +
  geom_bar(stat="count", width=0.7, fill = "red") +
  theme_minimal() + labs(title="Support for NAFTA among the House Republicans in Nov 1993", 
                         x="Support for NAFTA (0 - Oppose, 0.25 - Leaning against, \n 0.5 - Undecided, 0.75 - Leaning in favor, 1 - Support)", y = "Count")+
  ylim(0, 150)





############### Final vote plot

dathousedem <- na.omit(subset(dat, democrat==1 & senate==0, select = finalvote))

ggplot(dathousedem, aes(x=factor(finalvote))) +
  geom_bar(stat="count", width=0.7, fill = "steelblue") +
  theme_minimal() + labs(title="Final votes on NAFTA among the House Democrats in Nov 1993", 
                         x="Support for NAFTA (0 - Oppose, 1 - Support)", y = "Count") 



dathouserep <- na.omit(subset(dat, democrat==0 & senate==0, select = finalvote))

ggplot(dathouserep, aes(x=factor(finalvote))) +
  geom_bar(stat="count", width=0.7, fill = "red") +
  theme_minimal() + labs(title="Final votes on NAFTA among the House Republicans in Nov 1993", 
                         x="Support for NAFTA (0 - Oppose, 1 - Support)", y = "Count") 


################ Other variables cleaning


dat$conservation <- dat$conservation/100


dat$labormoneylog <- NA

for(i in 1:nrow(dat)){
  if(dat$labormoney2[i] == 0){dat$labormoneylog[i] <- 0}
  else if(dat$labormoney2[i] < 0){dat$labormoneylog[i] <- -(log(abs(dat$labormoney2[i])))}
  else{dat$labormoneylog[i] <- log(dat$labormoney2[i])}
}


dat$compete <- NA

for(i in 1:nrow(dat)){
  if(dat$democrat[i] == 1 & dat$dem_voteshare[i] < 0.6){dat$compete[i] <- 1}
  else if(dat$democrat[i] == 0 & dat$rp_voteshare[i] < 0.6){dat$compete[i] <- 1}
  else{dat$compete[i] <- 0}
}

dat$compete2 <- NA
for(i in 1:nrow(dat)){
  if(dat$dem_voteshare[i] < 0.58 & dat$democrat[i] == 1){dat$compete2[i] <- 1}
  else if(dat$rp_voteshare[i] < 0.58 & dat$democrat[i] == 0){dat$compete2[i] <- 1}
  else{dat$compete2[i] <- 0}}



dat$compete3 <- NA
for(i in 1:nrow(dat)){
  if(dat$dem_voteshare[i] < 0.57 & dat$democrat[i] == 1){dat$compete3[i] <- 1}
  else if(dat$rp_voteshare[i] < 0.57 & dat$democrat[i] == 0){dat$compete3[i] <- 1}
  else{dat$compete3[i] <- 0}}

compete4 <- NA
for(i in 1:nrow(dat)){
  if(dat$dem_voteshare[i] < 0.56 & dat$democrat[i] == 1){dat$compete4[i] <- 1}
  else if(dat$rp_voteshare[i] < 0.56 & dat$democrat[i] == 0){dat$compete4[i] <- 1}
  else{dat$compete4[i] <- 0}}

dat$compete5 <- NA
for(i in 1:nrow(dat)){
  if(dat$dem_voteshare[i] < 0.55 & dat$democrat[i] == 1){dat$compete5[i] <- 1}
  else if(dat$rp_voteshare[i] < 0.55 & dat$democrat[i] == 0){dat$compete5[i] <- 1}
  else{dat$compete5[i] <- 0}}


dat$compete6 <- NA
for(i in 1:nrow(dat)){
  if(dat$dem_voteshare[i] < 0.52 & dat$democrat[i] == 1){dat$compete6[i] <- 1}
  else if(dat$rp_voteshare[i] < 0.52 & dat$democrat[i] == 0){dat$compete6[i] <- 1}
  else{dat$compete6[i] <- 0}}

dat$compete7 <- NA
for(i in 1:nrow(dat)){
  if(dat$dem_voteshare[i] < 0.51 & dat$democrat[i] == 1){dat$compete7[i] <- 1}
  else if(dat$rp_voteshare[i] < 0.51 & dat$democrat[i] == 0){dat$compete7[i] <- 1}
  else{dat$compete7[i] <- 0}}

dat$importworkers <- 1/(dat$ratio)



################ Subset and Variable names
require(data.table)

setnames(dat, "conservation", "Environment") # LCV score lifetime score captured in 1993
setnames(dat, "democrat", "Democrat") 
setnames(dat, "labormoneylog", "LaborPAC")
setnames(dat, "senate", "Senate")
setnames(dat, "hhiIMP1", "Importcompeting") # Herfindahl-Hirschman index for import-competing industries
setnames(dat, "hhiEXP1", "Export") # Herfindahl-Hirschman index for exporting industries
setnames(dat, "importworkers", "Workers") # proportion of employees in import-competing vs. exporting industries
setnames(dat, "skill", "Education") # % of adults with a college degree
setnames(dat, "compete", "Competition") # incumbents who won the 1992 election with less than a 60% vote share
setnames(dat, "nominate_dim1", "Conservatism") # DW-NOM ideology score dimension 1

# Just using house data
dathouse <- dat[dat$Senate==0,]

head(dathouse)
subdat <- subset(dathouse, select = c('march', 'june', 'sep', 'oct', 'nov',
                            'finalvote', 'Environment', 'Competition',
                            'Importcompeting', 'Export', 'Workers', 
                            'LaborPAC', 'Democrat', 'Education', 'Conservatism'))

summary(subdat)
#stargazer(subdat, digits=2, style = "IO")
sd(dathouse$LaborPAC)
################ Table 2 OLS results 

# Outcome is attitude change (increased support) from June to September.
dathouse$Linkage <- dathouse$sep - dathouse$june


## Economic Protectionist hypothesis  -- M1
m_ep1 <- lm(Linkage ~  Importcompeting + LaborPAC + Democrat , data=dathouse)
summary(m_ep1)


## Economic Protectionist hypothesis  -- M2
m_ep2 <- lm(Linkage ~  Workers + LaborPAC + Democrat , data=dathouse)
summary(m_ep2)



# Economic Protectionist hypothesis  -- M1 FE (appendix)
m_ep3 <- lm(Linkage ~  Importcompeting + LaborPAC + Democrat + factor(state), data=dathouse)
summary(m_ep3)



# Economic Protectionist hypothesis  -- M2 FE (appendix)
m_ep4 <- lm(Linkage ~  Workers + LaborPAC + Democrat + factor(state), data=dathouse)
summary(m_ep4)


## Sustainable Trade hypothesis -- M3

m_st1 <- lm(Linkage ~  Environment + LaborPAC + Democrat , data=dathouse)
summary(m_st1)


## Sustainable Trade hypothesis -- M4
m_st2 <- lm(Linkage ~  Export + Environment + Environment*Export + LaborPAC + Democrat , data=dathouse)
summary(m_st2)

## Sustainable Trade hypothesis -- M4 FE (Appendix)
m_st3 <- lm(Linkage ~  Environment + Export + Environment*Export + LaborPAC + Democrat + factor(state) , data=dathouse)
summary(m_st3)


## Electoral Advantage hypothesis -- M5
m_ea1 <- lm(Linkage ~  Environment + Competition + Environment*Competition + LaborPAC + Democrat , data=dathouse)
summary(m_ea1)



## Electoral Advantage hypothesis -- M5 FE (Appendix)
m_ea2 <- lm(Linkage ~  Environment + Competition + Environment*Competition + LaborPAC + Democrat + factor(state) , data=dathouse)
summary(m_ea2)


m_ea_import <- lm(Linkage ~  Environment + Competition + Environment*Competition + 
              LaborPAC + Democrat + Importcompeting + Export, data=dathouse)
summary(m_ea_import)

m_ea_import_fe <- lm(Linkage ~  Environment + Competition + Environment*Competition + 
                    LaborPAC + Democrat + Importcompeting + Export + factor(state), data=dathouse)
summary(m_ea_import_fe)

m_ea_import2 <- lm(Linkage ~  Environment + Competition + Environment*Competition + 
                    LaborPAC + Democrat + Workers, data=dathouse)
summary(m_ea_import2)

m_ea_import2_fe <- lm(Linkage ~  Environment + Competition + Environment*Competition + 
                     LaborPAC + Democrat + Workers + factor(state), data=dathouse)
summary(m_ea_import2_fe)

#stargazer(m_ea_import, m_ea_import_fe, m_ea_import2, m_ea_import2_fe, digits=2, style="IO")
## Table 2
#stargazer(m_ep1, m_ep2, m_st1, m_st2, m_ea1, digits=2, style="IO")

## Appendix A.
m_st4 <- lm(Linkage ~   Education + Export + Education*Export + LaborPAC + Democrat , data=dathouse)
summary(m_st4)
#stargazer(m_st4, digits=2, style = "IO")

## Appendix B.
m_ea3 <- lm(Linkage ~  Environment + Competition + Environment*Competition + 
              LaborPAC + Democrat +  Education , data=dathouse)
summary(m_ea3)


m_ea4 <- lm(Linkage ~  Environment + Competition + Environment*Competition + 
              LaborPAC + Democrat +  Education + Conservatism, data=dathouse)
summary(m_ea4)


m_ea5 <- lm(Linkage ~  Environment + Competition + Environment*Competition + 
              LaborPAC + Democrat +  Education + Conservatism + factor(state), data=dathouse)
summary(m_ea5)

#stargazer(m_ea3, m_ea4, m_ea5, digits=2, style="IO")


m_ea6 <- lm(Linkage ~  Environment + compete2 + Environment*compete2 + 
              LaborPAC + Democrat , data=dathouse)
summary(m_ea6) # -- Appendix A


m_ea7 <- lm(Linkage ~  Environment + compete3 + Environment*compete3 + 
              LaborPAC + Democrat , data=dathouse)
summary(m_ea7) # -- Appendix A

m_ea8 <- lm(Linkage ~  Environment + compete4 + Environment*compete4 + 
              LaborPAC + Democrat , data=dathouse)
summary(m_ea8) # -- Appendix A

m_ea9 <- lm(Linkage ~  Environment + compete5 + Environment*compete5 + 
              LaborPAC + Democrat , data=dathouse)
summary(m_ea9) # -- Appendix A

m_ea10 <- lm(Linkage ~  Environment + compete6 + Environment*compete6 + 
              LaborPAC + Democrat , data=dathouse)
summary(m_ea10) # -- Appendix A

m_ea11 <- lm(Linkage ~  Environment + compete7 + Environment*compete7 + 
               LaborPAC + Democrat , data=dathouse)
summary(m_ea11) # -- Appendix A

#stargazer(m_ea6, m_ea7, m_ea8, m_ea9, m_ea10, m_ea11, digits=2, style = "IO")

dathouse$Environment93 <- dathouse$conservation_93/100
m_ea11 <- lm(Linkage ~  Environment93 + Competition + Environment93*Competition + 
               LaborPAC + Democrat , data=dathouse)
summary(m_ea11) # -- Appendix

m_ea12 <- lm(Linkage ~  Environment93 + Competition + Environment93*Competition + 
               LaborPAC + Democrat + Education + Conservatism , data=dathouse)
summary(m_ea12) # -- Appendix 

m_ea13 <- lm(Linkage ~  Environment93 + Competition + Environment93*Competition + 
               LaborPAC + Democrat + Education + Conservatism + factor(state) , data=dathouse)
summary(m_ea13) # -- Appendix 


#stargazer(m_ea11, m_ea12, m_ea13, digits=2, style="IO")


## Marginal Effect Plot 1
interplot(m = m_ea1, 
          var1 = 'Competition', 
          var2 = 'Environment',
          hist = TRUE) +theme(plot.title = element_text(face='bold'))  + 
          theme_bw()+
        xlab("Environmental Commitment") +
        ylab("Electoral Competition Coefficient")   +  
        geom_hline(yintercept = 0, linetype = "dashed")




## Footnote on LCV score lifetime vs. annual snapshot
dathouse$Environment_93 <- dathouse$conservation_93/100
cor(dathouse$Environment_93, dathouse$Environment,  method = "pearson", use = "complete.obs")
# high correlation = 0.89



####### Table 3

## Economic protectionism hypothesis
f_ep1 <- glm(finalvote ~ june + LaborPAC+ Importcompeting + Democrat + LaborPAC,
          data = dathouse, family = "binomial")
summary(f_ep1)

f_ep2 <- glm(finalvote ~ june + LaborPAC+ Workers + Democrat + LaborPAC,
          data = dathouse, family = "binomial")
summary(f_ep2)



## Sustainable trade hypothesis -- actually finds some results
f_st1 <- glm(finalvote ~ june + Environment + Democrat + LaborPAC,
             data = dathouse, family = "binomial")
summary(f_st1)

f_st2 <- glm(finalvote ~  june + Environment + Export + Environment*Export + Democrat + LaborPAC,
             data = dathouse, family = "binomial")
summary(f_st2)


f_st3 <- glm(finalvote ~  june + Education + Export + Education*Export + Democrat + LaborPAC,
             data = dathouse, family = "binomial")
summary(f_st3)

#stargazer(f_st3, digits=2, style = "IO")


f_ea1 <- glm(finalvote ~ june + Environment + Competition + Environment*Competition + Democrat + LaborPAC,
             data = dathouse, family = "binomial")
summary(f_ea1)

f_ea2 <- glm(finalvote ~ june + Environment + Competition + Environment*Competition + 
               Democrat + LaborPAC + Education,
             data = dathouse, family = "binomial")
summary(f_ea2)

f_ea3 <- glm(finalvote ~ june + Environment + Competition + Environment*Competition + 
               Democrat + LaborPAC + Education + Conservatism,
             data = dathouse, family = "binomial")
summary(f_ea3)

f_ea4 <- glm(finalvote ~ june + Environment + Competition + Environment*Competition + 
               Democrat + LaborPAC + Education + Conservatism + factor(state),
             data = dathouse, family = "binomial")
summary(f_ea4)


f_ea4_import1 <- glm(finalvote ~ june + Environment + Competition + Environment*Competition + 
               Democrat + LaborPAC + Importcompeting + Export,
             data = dathouse, family = "binomial")
summary(f_ea4_import1)

f_ea4_import1_fe <- glm(finalvote ~ june + Environment + Competition + Environment*Competition + 
                       Democrat + LaborPAC + Importcompeting + Export + factor(state),
                     data = dathouse, family = "binomial")
summary(f_ea4_import1_fe)


f_ea4_import2 <- glm(finalvote ~ june + Environment + Competition + Environment*Competition + 
                          Democrat + LaborPAC + Workers ,
                        data = dathouse, family = "binomial")
summary(f_ea4_import2)

f_ea4_import2_fe <- glm(finalvote ~ june + Environment + Competition + Environment*Competition + 
                          Democrat + LaborPAC + Workers + factor(state),
                        data = dathouse, family = "binomial")
summary(f_ea4_import2_fe)

#stargazer(f_ea4_import1, f_ea4_import1_fe, f_ea4_import2, f_ea4_import2_fe, digits=2, style = "IO")


# electoral competition threshold
f_ea5 <- glm(finalvote ~ june + Environment + compete2 + Environment*compete2 + 
               Democrat + LaborPAC ,
             data = dathouse, family = "binomial")
summary(f_ea5)


f_ea6 <- glm(finalvote ~ june + Environment + compete3 + Environment*compete3 + 
               Democrat + LaborPAC,
             data = dathouse, family = "binomial")
summary(f_ea6)


f_ea7 <- glm(finalvote ~ june + Environment + compete4 + Environment*compete4 + 
               Democrat + LaborPAC ,
             data = dathouse, family = "binomial")
summary(f_ea7)


f_ea8 <- glm(finalvote ~ june + Environment + compete5 + Environment*compete5 + 
               Democrat + LaborPAC ,
             data = dathouse, family = "binomial")
summary(f_ea8)


f_ea9 <- glm(finalvote ~ june + Environment + compete6 + Environment*compete6 + 
               Democrat + LaborPAC ,
             data = dathouse, family = "binomial")
summary(f_ea9)


f_ea10 <- glm(finalvote ~ june + Environment + compete7 + Environment*compete7 + 
               Democrat + LaborPAC ,
             data = dathouse, family = "binomial")
summary(f_ea10)


#stargazer(f_ep1, f_ep2, f_st1, f_st2, f_ea1, digits=2, style = "IO")
#stargazer(f_ea2, f_ea3, f_ea4, digits=2, style = "IO")
#stargazer(f_ea5, f_ea6, f_ea7, f_ea8, f_ea9, f_ea10, digits=2, style = "IO")



f_ea11 <- glm(finalvote ~ june + Environment93 + Competition + Environment93*Competition + 
                Democrat + LaborPAC ,
              data = dathouse, family = "binomial")
summary(f_ea11)

f_ea12 <- glm(finalvote ~ june + Environment93 + Competition + Environment93*Competition + 
                Democrat + LaborPAC + Education + Conservatism,
              data = dathouse, family = "binomial")
summary(f_ea12)

f_ea13 <- glm(finalvote ~ june + Environment93 + Competition + Environment93*Competition + 
                Democrat + LaborPAC + Education + Conservatism + factor(state),
              data = dathouse, family = "binomial")
summary(f_ea13)

#stargazer(f_ea11, f_ea12, f_ea13, digits=2, style= "IO")

## Marginal Effect Plot 2


plot1 <- interplot(m = f_ea1, 
          var1 = 'Competition', 
          var2 = 'Environment',
          predPro = TRUE,
          ci = 0.95,
          var2_vals = c(min(dathouse$Environment, na.rm=T),max(dathouse$Environment, na.rm=T)),
          sim=10000,
          xmin = 0,
          xmax = 1,
          hist = TRUE) +theme(plot.title = element_text(face='bold'))  


plot1 +  theme_bw() + theme(legend.position = "none") + 
   scale_fill_manual(values=c("grey", "red")) + xlab("Environmental Commitment") +
  ylab("Predicted Probability of Supporting \n NAFTA after the Side Agreement")


interplot(m = f_ea1, 
          var1 = 'Competition', 
          var2 = 'Environment',
          ci = 0.95,
          sim=10000,
          hist = TRUE) +theme(plot.title = element_text(face='bold'))  + 
  theme_bw()+
  xlab("Environmental Commitment") +
  ylab("Marginal Effect of Electoral Competition on Increase of Pro-NAFTA Support") +  
  geom_hline(yintercept = 0, linetype = "dashed")  




##### Appendix

dathouse$jm <- dathouse$june - dathouse$march


m0 <- lm(jm ~  Importcompeting + LaborPAC + Democrat , data=dathouse)
summary(m0)

m01 <- lm(jm ~  Workers + LaborPAC + Democrat , data=dathouse)
summary(m01)

m1 <- lm(jm ~  Environment  + LaborPAC + Democrat, data=dathouse)
summary(m1)


m2_mar_june <- lm(jm ~  Environment + Competition + Environment*Competition + Democrat + Education + Conservatism + LaborPAC, data=dathouse)
summary(m2_mar_june)



######################################## 9 ~ 10

dathouse$os <- dathouse$oct - dathouse$sep


m0 <- lm(os ~  Importcompeting + LaborPAC + Democrat , data=dathouse)
summary(m0)


m1 <- lm(os ~  Environment  + LaborPAC + Democrat, data=dathouse)
summary(m1)


m2_sep_oct <- lm(os ~  Environment + Competition + Environment*Competition + 
                   Democrat + Education + Conservatism + LaborPAC, data=dathouse)
summary(m2_sep_oct)


########### 10 ~ 11
dathouse$no <- dathouse$nov - dathouse$oct


m2_oct_nov <- lm(no ~  Environment + Competition + Environment*Competition  +  
                   LaborPAC + Democrat + Education + Conservatism, data=dathouse)

summary(m2_oct_nov)
#stargazer(m2_mar_june,  m2_sep_oct, m2_oct_nov,digits=2, style = "IO")



#stargazer(m2_mar_june, m2_sep_oct, m2_oct_nov,  digits=2, style = "IO")


dathouse$no <- dathouse$nov - dathouse$oct


dathouse$nm <- dathouse$nov - dathouse$march

m2_nov_mar <- lm(nov ~  june + Environment + LaborPAC  + Environment*Competition + 
                   Workers + Democrat + Competition , data=dathouse)

summary(m2_nov_mar)




########### APPENDIX 
f_ea1
newdata1 <- with(dathouse, data.frame(june = mean(june, na.rm=T), 
                                      Environment = rep(seq(from = 0, to = 1, length.out = 100),
                                                        2), 
                                      Competition = (rep(0:1, each = 100)), 
                                      Democrat = 1, LaborPAC = mean(dathouse$LaborPAC, na.rm=T)))

newdata3 <- cbind(newdata1, predict(f_ea1, newdata = newdata1, type = "link",
                                    se = TRUE))

newdata3 <- within(newdata3, {
  PredictedProb <- plogis(fit)
  LL <- plogis(fit - (1.96 * se.fit))
  UL <- plogis(fit + (1.96 * se.fit))
})

newdata3$PredictedProb <- newdata3$PredictedProb * 100
newdata3$UL <- newdata3$UL*100
newdata3$LL <- newdata3$LL*100

head(newdata3)

ggplot(newdata3, aes(x = Environment, y = PredictedProb)) + 
           geom_ribbon(aes(ymin = LL, ymax = UL, fill = factor(Competition)), alpha = 0.2) + 
           geom_line(aes(colour = factor(Competition)), size = 1) + 
          theme_bw() + theme(legend.position = "top") +
   xlab("Environmental Commitment (0 - 1)") + 
  ylab("Predicted Probability of Supporting \n NAFTA after the Side Agreement (%)")  





####### Appendix H

hist(dathouse$LaborPAC)
hist(dathouse$environment_Sep)


dathouse %>%
  group_by(environment_Sep) %>%
  summarise(total = mean(labormoney2))

cor(dathouse$environment_Sep, dathouse$Importcompeting,  method = "pearson", use = "complete.obs")
cor(dathouse$environment_Sep, dathouse$Export,method = "pearson", use = "complete.obs")

cor(dathouse$environment_Sep, dathouse$Environment,  method = "pearson", use = "complete.obs")
cor(dathouse$environment_Sep, dathouse$Workers,  method = "pearson", use = "complete.obs")


